#/bin/bash

startday=$1
endday=$2

for ((day=$startday; day<=$endday; day++)) 
do 
    echo "[info] downloading day 0$day's log ..."
    wget ftp://iptv_vis_squidlog:iptv_vis_squidlog@124.108.10.62/Fuzhou/squid_log_2013040$day*.tar.gz
    echo "unziping day 0$day's log... "
    tar -zxvf squid_log_2013040$day*.tar.gz
    echo "merging day 0$day's log..."
    cat transform* | grep ToProgram >> squid_log_2013040$day.merged
    echo "removing day 0$day's log..."
    rm -f transform*
    rm -f squid_log_2013040$day*.tar.gz
done


echo '[info] converting to csv file ...'
awk -F'?' '{print $2}' squid_log_201304*.merged |
awk -F' HTTP/1.1' '{print $1}' |
awk '{split($0,arr,"&"); print(substr(arr[2],index(arr[2],"=")+1) "," substr(arr[3],index(arr[3],"=")+1) "," substr(arr[4],index(arr[4],"=")+1) "," substr(arr[5],index(arr[5],"=")+1) "," substr(arr[6],index(arr[6],"=")+1) "," substr(arr[1],index(arr[1],"=")+1) ",")} ' > clicklog.csv

